package com.mbuswidget;

import java.util.Collections;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MBusWidgetServiceConfig extends Activity {
	
	/** Some text view we are using to show state information. */
	TextView mCallbackText;
	ToggleButton useNetworkLocationToggle;
	ToggleButton useGPSLocationToggle;
	ToggleButton autoToggleButton;
	Button serviceConfig;
	SharedPreferences prefs;
	EditText autoInterval;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		setResult(RESULT_CANCELED);
		
//        Log.w("mbuswidget", "Starting mbus application configuration");
       
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
				
		Button stopService = (Button)findViewById(R.id.stopService);
		stopService.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intentShutdown = new Intent(MBusWidgetServiceConfig.this, MBusWidgetService.class);
				intentShutdown.putExtra(MBusWidgetConstants.FORCE_RESTART, true);
				startService(intentShutdown);
			}
		});
		
		Button startService = (Button)findViewById(R.id.startService);
		startService.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intentShutdown = new Intent(MBusWidgetServiceConfig.this, MBusWidgetService.class);
				intentShutdown.putExtra(MBusWidgetConstants.SERVICE_ANNOUNCE, true);
				startService(intentShutdown);
			}
		});
		
		useNetworkLocationToggle = (ToggleButton) findViewById(R.id.useNetworkToggle);
		useNetworkLocationToggle.setChecked(MBusWidgetConstants.GetNetworkLocationEnabled(prefs));
		useNetworkLocationToggle.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        // Perform action on clicks
		        MBusWidgetConstants.SetNetworkLocationEnabled(prefs, useNetworkLocationToggle.isChecked());
		        
		        // TODO: Remove Toast
//		        Toast.makeText(getApplicationContext(), "Use Network location is " + (MBusWidgetConstants.GetNetworkLocationEnabled(prefs) ? "on" : "off"), Toast.LENGTH_SHORT).show();
		    }
		});
		
		useGPSLocationToggle = (ToggleButton) findViewById(R.id.useGPSToggle);
		useGPSLocationToggle.setChecked(MBusWidgetConstants.GetGPSLocationEnabled(prefs));
		useGPSLocationToggle.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        // Perform action on clicks
		        MBusWidgetConstants.SetGPSLocationEnabled(prefs, useGPSLocationToggle.isChecked());
		        
		        // TODO: Remove Toast
//		        Toast.makeText(getApplicationContext(), "Use GPS location is " + (MBusWidgetConstants.GetGPSLocationEnabled(prefs) ? "on" : "off"), Toast.LENGTH_SHORT).show();
		    }
		});
		
		autoToggleButton = (ToggleButton) findViewById(R.id.automaticDownloadToggle);
		autoToggleButton.setChecked(MBusWidgetConstants.GetDownloadIntervalEnabled(prefs));
		autoToggleButton.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        // Perform action on clicks
		        MBusWidgetConstants.SetDownloadIntervalEnabled(prefs, autoToggleButton.isChecked());
		        
		        // TODO: Remove Toast
//		        Toast.makeText(getApplicationContext(), "Auto Update is " + (MBusWidgetConstants.GetDownloadIntervalEnabled(prefs) ? "on" : "off"), Toast.LENGTH_SHORT).show();
		    }
		});
		
		autoInterval = (EditText)findViewById(R.id.automaticDownloadInterval);
		Integer autoInt = MBusWidgetConstants.GetDownloadInterval(prefs);
		autoInterval.setText(autoInt.toString());
		autoInterval.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				try {
					int value = Integer.parseInt(s.toString());
					MBusWidgetConstants.SetDownloadInterval(prefs, value);
				} catch (Exception e) {

				}
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
